﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.PrintExport;
using System.Threading;

namespace Aliyeye.Car.Net.UI.SystemModel
{
    public partial class RemindList : Aliyeye.Car.Net.UI.BaseForm
    {
        CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();
        CarSvc.RemindListInfo[] list = null;

        public RemindList()
        {
            InitializeComponent();
        }

        private void Role_Load(object sender, EventArgs e)
        {
            dgvBind();
        }

        private void dgvBind()
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            dgvInfo.DataSource = null;
            dgvInfo.AutoGenerateColumns = false;
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                            proxy => proxy.GetRemindList(sopaHeader),
                            result =>
                            {
                                if (result.Error == null)
                                {
                                    list = (CarSvc.RemindListInfo[])result.Result;
                                    dgvInfo.DataSource = result.Result;

                                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                                }
                                else
                                {
                                    PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                                }
                            },
                            this
                        );
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "截止时间")
            {
                if (e.Value != null && !string.IsNullOrEmpty(e.Value.ToString()))
                {
                    DateTime dtEnd = Convert.ToDateTime(e.Value.ToString());
                    DateTime dtNow = DateTime.Now;
                    if (dtNow.Subtract(dtEnd).Days > 0)
                        this.dgvInfo.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    else
                        this.dgvInfo.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
                }
            }
        }

        //刷新
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            dgvBind();
        }

        //导出
        private void btnExport_Click(object sender, EventArgs e)
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                ExcelHelper excelHelper = new ExcelHelper();
                string msg = "";
                if (excelHelper.RemindListExport(list, filePath,ref msg))
                {
                    PageCommon.Notify("文档导出成功！");
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
                else
                {
                    PageCommon.Alert(msg);
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
            }
        }

        //打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            //WordHelper wordHelper = new WordHelper();
            //object info = new object();
            //ThreadPool.QueueUserWorkItem(new WaitCallback(wordHelper.PrintAndSaveWord), info);

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            if (list != null && list.Length > 0)
            {
                ExcelHelper excelHelper = new ExcelHelper();
                string msg = "";
                if (excelHelper.RemindListPrint(list, ref msg))
                {
                    PageCommon.Notify("文档打印成功！");
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
                else
                {
                    PageCommon.Alert(msg);
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
            }
        }

        //返回
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
